*** Settings ***
Documentation    用户增删改查测试用例
Library    SeleniumLibrary
Library    RequestsLibrary

Suite Setup      Open Browser To Login Page
Suite Teardown   Close All Browsers


*** Variables ***
${URL}           http://127.0.0.1:8001
${ADD_URL}       ${URL}/add
${EDIT_URL}      ${URL}/edit
${DELETE_URL}    ${URL}/remove/%d

${USER_NAME}     TestUserRobot
${UPDATED_NAME}  UpdatedNameRobot
${AGE}           30
${GENDER}        男
${PHONE}         1234567890

*** Keywords ***
Open Browser To Login Page
    Open Browser    ${URL}    chrome
    Maximize Browser Window

Wait For Success Message
    Wait Until Element Is Visible    id=success-message    timeout=10s

Clear Search Input
    ${exists}=    Run Keyword And Return Status
    ...    Element Should Be Visible    name=keyword
    Run Keyword If    '${exists}' == 'True'
    ...    Clear Element Text    name=keyword

*** Test Cases ***

TC01_新增用户
    [Documentation]    验证能否成功添加新用户
    Click Link    link=新增
    Input Text    name=name    ${USER_NAME}
    Input Text    name=age    ${AGE}
    Click Element    xpath=//input[@value='男']
    Input Text    name=phone    ${PHONE}
    Click Button    xpath=//button[contains(text(), '保存')]
    Wait For Success Message
    Page Should Contain    User added successfully

TC02_编辑用户
    [Documentation]    验证能否成功编辑用户
    Go To    ${URL}
    Click Link    xpath=(//a[contains(text(),'编辑')])[1]
    Clear Element Text    name=name
    Input Text    name=name    ${UPDATED_NAME}
    Click Button    xpath=//button[contains(text(), '保存')]
    Wait For Success Message
    Page Should Contain    updated

TC03_搜索用户
    [Documentation]    验证搜索功能是否正常
    Go To    ${URL}
    Clear Search Input
    Input Text    name=keyword    ${UPDATED_NAME}
    Click Button    id=button-addon1
    Wait Until Element Is Visible    xpath=//table//tr[td[contains(.,'${UPDATED_NAME}')]]
    Element Should Be Visible    xpath=//table//tr[td[contains(.,'${UPDATED_NAME}')]]

TC04_删除用户
    [Documentation]    验证能否成功删除用户
    Go To    ${URL}
    Click Link    xpath=(//a[contains(text(),'删除')])[1]
    Handle Alert    ACCEPT
    Wait For Success Message
    Page Should Contain    deleted